﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Legend.Object;
using Legend.Component.Common;
using Legend.Component.Monster;
using Legend.Component.Interface;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.IO;

namespace LegendMain.TestBed
{
    struct MonsterGenerationJob
    {
        public int ActivationTime;
        public GameObject Monster;
    };

    public static class MonsterGenerator
    {
        private static ContentManager contentManager;
        private static Queue<MonsterGenerationJob> jobQueue = new Queue<MonsterGenerationJob>();
        private static Dictionary<string, Model> models = new Dictionary<string,Model>();

        private static Model FindModel(string model)
        {
            if (!models.ContainsKey(model))
            {
                models[model] = contentManager.Load<Model>(model);
            }

            return models[model];
        }

        public static void Initialize(ContentManager manager)
        {
            contentManager = manager;
            char[] seperator = new char[] { ',' };

            using (StreamReader reader = new StreamReader(File.Open("MonsterDeploy.txt", FileMode.Open)))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    line.Trim();

                    if (line.StartsWith("//"))
                    {
                        continue;
                    }

                    string[] tokens = line.Split(seperator);
                    if (tokens.Length < 4)
                    {
                        continue;
                    }

                    GameObject mon = new GameObject();
                    string modelName = tokens[0].Trim();

                    if (tokens.Length == 6 && string.Compare(tokens[5].Trim(), "sign", true) == 0)
                    {
                        SignwaveMovement movement = mon.CreateComponent<SignwaveMovement>();
                        movement.Speed = -200;
                        movement.Multifier = 250;
                        movement.Frequency = 4000;
                        movement.Position = new Vector3(float.Parse(tokens[2]), float.Parse(tokens[3]), 30);
                    }
                    else
                    {
                        LinearMovement movement = mon.CreateComponent<LinearMovement>();
                        movement.Velocity = new Vector3(0, -150, 0);
                        movement.Position = new Vector3(float.Parse(tokens[2]), float.Parse(tokens[3]), 30);
                    }

                    IMovement imovement = mon.FindComponent<IMovement>();
                    if (modelName == "mon01")
                    {
                        imovement.Rotate(Vector3.Backward, (float)Math.PI / 2);
                    }
                    else if (modelName == "mon02")
                    {

                    }


                    SimpleRenderable renderable = mon.CreateComponent<SimpleRenderable>();
                    renderable.Model = FindModel(modelName);
                    Collision collision = mon.CreateComponent<Collision>();
                    collision.LocalBoundingBox = Legend.Helper.Mesh.GetModelBoundingBox(renderable.Model);
                    MonsterDamaging damaging = mon.CreateComponent<MonsterDamaging>();
                    damaging.Damage = 10;

                    MonsterGenerationJob job;
                    job.ActivationTime = int.Parse(tokens[1]);
                    job.Monster = mon;

                    jobQueue.Enqueue(job);
                }

                //for (int i = 0; i < 8; ++i)
                //{
                //    GameObject mon = new GameObject();
                //    int activateTime = 15000 + i * 300;

                    

                //    //movement.Velocity = new Vector3(0, -150, 0);
                //    //movement.Position = new Vector3((float)(Math.Sin((double)activateTime / 3000 * 2 * Math.PI) * 200) , 1000, 30);
                    
                //    SimpleRenderable renderable = mon.CreateComponent<SimpleRenderable>();
                //    renderable.Model = FindModel("mon01");
                //    Collision collision = mon.CreateComponent<Collision>();
                //    collision.LocalBoundingBox = Legend.Helper.Mesh.GetModelBoundingBox(renderable.Model);
                //    MonsterDamaging damaging = mon.CreateComponent<MonsterDamaging>();
                //    damaging.Damage = 10;

                //    MonsterGenerationJob job;
                //    job.ActivationTime = activateTime;
                //    job.Monster = mon;

                //    jobQueue.Enqueue(job);
                //}

            }
        }

        public static void Update(GameTime gameTime)
        {
            while (jobQueue.Count > 0)
            {
                if (jobQueue.Peek().ActivationTime <= (int)(gameTime.TotalGameTime.TotalMilliseconds))
                {
                    MonsterGenerationJob job = jobQueue.Dequeue();

                    GameObjectManager.RegistGameObject(job.Monster);
                }
                else
                {
                    break;
                }
            }
        }

    }
}
